def filter_sg():
    sg_venue = {}
    sg_checkin = []
    with open('dataset_TIST2015_POIs.txt') as poi_file:
        line = poi_file.readline()
        while line:
            tokens = line.split()
            if tokens[-1] == "SG":
                sg_venue[tokens[0]]=tokens[1:]
            line = poi_file.readline()
    with open('dataset_TIST2015_Checkins.txt') as checkin_file:
        line=checkin_file.readline()
        while line:
            tokens = line.split()
            if tokens[1] in sg_venue.keys():
                venue = sg_venue[tokens[1]]
                tokens.insert(0,venue[1])
                tokens.insert(0,venue[0])
                for v in venue[2:]:
                    tokens.append(v)
                sg_checkin.append(tokens)
            line=checkin_file.readline()
    with open('checkin_sg.csv','w') as checkin_sg:
        for line in sg_checkin:
            checkin_sg.write(','.join(line)+'\n')

LONDON_LAT=(51.29582756526526,51.688259477147454)
LONDON_LON=(-0.4984198459021278,0.28682231111241074)

def filter_ld():
    ld_venue = {}
    ld_checkin = []
    with open('dataset_TIST2015_POIs.txt') as poi_file:
        line = poi_file.readline()
        while line:
            tokens = line.split()
            geos = (float(tokens[1]),float(tokens[2]))
            # print(geos)
            if LONDON_LAT[0] < geos[0]<LONDON_LAT[1] and LONDON_LON[0]<geos[1]<LONDON_LON[1]:
                ld_venue[tokens[0]]=tokens[1:]
            line = poi_file.readline()
    print(len(ld_venue))
    with open('dataset_TIST2015_Checkins.txt') as checkin_file:
        line=checkin_file.readline()
        while line:
            tokens = line.split()
            if tokens[1] in ld_venue.keys():
                venue = ld_venue[tokens[1]]
                tokens.insert(0,venue[1])
                tokens.insert(0,venue[0])
                for v in venue[2:]:
                    tokens.append(v)
                ld_checkin.append(tokens)
            line=checkin_file.readline()
    with open('checkin_ld.csv','w') as checkin_sg:
        for line in ld_checkin:
            checkin_sg.write(','.join(line)+'\n')

if __name__=="__main__":
    filter_sg()